package org.biodas.jdas.client;

import org.biodas.jdas.exceptions.DASClientException;
import org.biodas.jdas.schema.types.DASTYPES;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ljgarcia
 * Date: 22-Feb-2011
 * Time: 10:51:03
 * To change this template use File | Settings | File Templates.
 */
public class TypesClient  {
    private DASTYPES fetchData(String serverURL, String arguments) throws DASClientException {
		JAXBContext jc;
		try {
			jc = JAXBContext.newInstance("org.biodas.jdas.schema.types");
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            String url;
            if (!serverURL.endsWith("/")) {
                url = serverURL + "/types" + arguments;
            } else {
                url = serverURL + "types" + arguments;
            }
			return (DASTYPES) unmarshaller.unmarshal(new URL(url));
		} catch (MalformedURLException e) {
			throw new DASClientException(e.getMessage(), e.getCause());
		} catch (JAXBException e) {
			throw new DASClientException(e.getMessage(), e.getCause());
		}
	}
	/**
	 * Retrieves all types, no arguments.
	 * @param serverURL
	 */
	public DASTYPES fetchData(String serverURL) throws DASClientException {
		return this.fetchData(serverURL, "");
	}

    /**
     * Retrieves the types response, arguments allowed.
     * @param serverURL
     * @param segments
     * @param types
     * @return
     * @throws MalformedURLException
     * @throws JAXBException
     */
	public DASTYPES fetchData(String serverURL, List<String> segments, List<String> types) throws DASClientException {
        String arguments = "";
        String segmentArguments = null;
        String typeArguments = null;
        if ((segments != null) && (segments.size() != 0)) {
            segmentArguments= "";
            for (String segment: segments) {
                segmentArguments += ";segment=" + segment;
            }
            segmentArguments = segmentArguments.substring(1); //we need to remove the first ;
        }
        if ((types != null) && (types.size() != 0)) {
            typeArguments = "";
            for (String type: types) {
                typeArguments += ";type=" + type;
            }
            typeArguments = typeArguments.substring(1); //we need to remove the first ;
        }
        if ((segmentArguments != null) && (typeArguments != null)) {
            arguments = arguments + "?" + segmentArguments + ";" + typeArguments;
        } else if ((segmentArguments != null) && (typeArguments == null)) {
            arguments = arguments + "?" + segmentArguments;
        } else if ((segmentArguments == null) && (typeArguments != null)) {
            arguments = arguments + "?" + typeArguments;
        }
        return this.fetchData(serverURL, arguments);
	}
}